#include "ELTReader.h"

ELTReader::ELTReader(const wxString &isoFile)
{
    m_isoFile = new wxFile(isoFile.c_str(), wxFile::read);
    m_buf = new wxUint8[SECTION_SIZE];
    m_bufSectionEnd = m_bufSectionStart = -1;
}

ELTReader::~ELTReader()
{
    delete m_isoFile;
    delete m_buf;
}

/**
 * sectionIndex start from 0.
 */
size_t ELTReader::ReadSection(size_t sectionIndex, wxUint8** ppBuf)
{
	if(!m_isoFile)
	{
		*ppBuf = NULL;
		return 0;
	}
	if(sectionIndex>=m_bufSectionStart && sectionIndex<=m_bufSectionEnd)
	{
		*ppBuf = (m_buf + (sectionIndex - m_bufSectionStart));
	}
	else
	{
		m_isoFile->Seek(sectionIndex*SECTION_SIZE, wxFromStart);
		m_isoFile->Read(m_buf, SECTION_SIZE);
		m_bufSectionStart = m_bufSectionEnd = sectionIndex;
		*ppBuf = m_buf;
	}
	return SECTION_SIZE;
}

wxFileOffset ELTReader::GetTotalLength()
{
	return m_isoFile->Length();
}
